import { CartItem } from "@/types/cart";
import { ApiRes } from "@/types/data";
import request from "@/utils/request";

/**
 * 加入购物车
 * @params skuId 
 * @params count 数量
 */
export function addCartListAPI (data: { skuId: string, count: number }) {
  return request<ApiRes<CartItem>>({
    url: '/member/cart',
    method: "post",
    data
  });
}

/**
 * 获取购物车列表
 * @returns 
 */
export function getCartListAPI () {
  return request<ApiRes<CartItem[]>>({
    url: '/member/cart',
    method: "get",
  });
}

/**
 * 删除购物车商品
 * @params ids id集合
 */
export function deleCartListAPI (ids: string[]) {
  return request({
    url: '/member/cart',
    method: "delete",
    data: {
      ids
    }
  });
}


/**
 * 修改购物车单品
 * @param skuId SKUID
 * @param data selected 选中状态 count 商品数量
 */
export const putMemberCartBySkuIdAPI = (
  skuId: string,
  data: { selected?: boolean; count?: number },
) => {
  return request({
    method: 'PUT',
    url: `/member/cart/${skuId}`,
    data,
  })
}


/**
 * 是否全选
 * @param data isSelected  是否全选
 */
export const putMemberCartAPI = (isSelected: boolean) => {
  return request({
    method: 'PUT',
    url: `/member/cart/selected`,
    data: {
      selected: isSelected
    }
  })
}


/**
 * 获取最新商品信息
 */
export function getCartBySkuIdAPI (skuId: string) {
  return request<ApiRes<CartItem>>({
    url: `/goods/stock/${skuId}`,
    method: "get",
  });
}


/**
 * 合并购物车
 * @data skuId:id, selected:是否选中，count:数量
 */
export function postMemberCartMergeAPI (data: {
  skuId: string
  selected: boolean
  count: number
}[]) {
  return request<ApiRes<CartItem>>({
    url: '/member/cart/merge',
    method: "post",
    data
  });
}